$(function () {
    var tableSource = new $.jqx.dataAdapter({
        dataType: "json",
        dataFields: [
            {name: "id", type: "int"},
            {name: "uuid", type: "string"},
            {name: "photo", type: "string"},
            {name: "text", type: "string"},
            {name: "path", type: "string"},
            {name: "type", type: "int"},
            {name: "isdeleted", type: "boolean"},
            {name: "createtime", type: "date"},
            {name: "creator", type: "string"}
        ],
        id: "id",
        url: "advertise/all.do",
        data: {isdeleted: 0, type: 1}
    });
    $("#dataTable").jqxDataTable({
        width: "100%",
        source: tableSource,
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            {
                text: "序号", width: 50, cellsRenderer: function (row) {
                return (row + 1).toString();
            }
            },
            {dataField: "photo", hidden: true},
            {text: "文字", dataField: "text", minWidth: 120},
            {
                text: "广告类型", dataField: "type", minWidth: 30,
                cellsRenderer: function (ruw, column, value, rowData) {
                    /**
                     *  '<option value="5">PC闪购</option>' +
                     '<option value="6">PC全球购</option>' +
                     '<option value="7">PC团购</option>' +
                     '<option value="8">PC折扣空间</option>' +
                     '<option value="9">wap幻灯片</option>' +
                     '<option value="10">wap闪购</option>' +
                     '<option value="11">wap全球购</option>' +
                     '<option value="12">wap团购</option>' +
                     '<option value="13">wap折扣空间</option>' +
                     * @type {Array}
                     */
                    var arr = new Array();
                    arr.push("PC幻灯片");
                    arr.push("公告顶部");
                    arr.push("首页横幅");
                    arr.push("搜索框右侧广告");
                    arr.push("PC闪购");
                    arr.push("PC全球购");
                    arr.push("PC团购");
                    arr.push("PC折扣空间");
                    arr.push("wap幻灯片");
                    arr.push("wap闪购");
                    arr.push("wap全球购");
                    arr.push("wap团购");
                    arr.push("wap折扣空间");
                    return arr[value - 1];
                }
            },
            {
                text: "状态", dataField: "isdeleted", width: 150,
                cellsRenderer: function (row, column, value, rowData) {
                    return value ? '<input type="button" class="btn btn-sm btn-danger" onclick="showOperator('+rowData.type+','+rowData.isdeleted+','+rowData.id+')" value="未展示" />'
                        : '展示中';
                }
            },
            {text: "创建者", dataField: "creator", width: 150},
            {
                text: "创建时间", dataField: "createtime", width: 150,
                cellsRenderer: function (row, column, value, rowData) {
                    return format(value, "Y-M-D H:I");
                }
            }
        ],
    });
    $("#dataTable").on();
    $("#dataTable").on("rowDoubleClick", function (info, args) {
        if (args && args.flag) {
            info = args;
        }
        editingIndex = info.args.index;
        editingData = info.args.row;
        _editerIndex = layer.open({
            id: "sellerEditer",
            title: "查看/编辑/添加广告信息",
            type: 1,
            skin: 'layui-layer-rim',
            area: ['1000px', '640px'],
            content:
            '<form id="admin-editer" enctype="multipart/form-data" method="post" class="form" style="height: 100%;overflow: auto;">' +
            //{1:"幻灯片",2:"公告顶部",3:"首页横幅",4:"搜索框右边广告"}
            '<br><label class="from-label" for="type">广告图类型：</label>'+
            '<select name="type" class="form-control" style="width:830px">' +
            '<option value="1">PC幻灯片</option>' +
            '<option value="2">公告顶部</option>' +
            '<option value="3">首页横幅</option>' +
            '<option value="4">搜索框右边广告</option>' +
            '<option value="5">PC闪购</option>' +
            '<option value="6">PC全球购</option>' +
            '<option value="7">PC团购</option>' +
            '<option value="8">PC折扣空间</option>' +
            '<option value="9">wap幻灯片</option>' +
            '<option value="10">wap闪购</option>' +
            '<option value="11">wap全球购</option>' +
            '<option value="12">wap团购</option>' +
            '<option value="13">wap折扣空间</option>' +
            '</select>'+
            '<input type="hidden" name="id" />' +
            '<input type="hidden" name="submit-url" value="advertise/update.do" />' +
            '<br><label class="from-label" for="text">文字：</label>' +
            '<input type="text" name="text" allowBlank="false" class="form-control" placeholder="文字" style="width:830px" />' +
            '<label class="from-label" allowBlank="false" for="path">Url：</label>' +
            '<input type="text" name="path" class="form-control" placeholder="例：http://www.baidu.com" style="width:830px" />' +
            '<label class="from-label" for="photo">图片：</label>' +
            '<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
            '<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
            '<img name="photo"/>' +
            '</div>' +
            '<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
            '<div id="uploadimg">' +
            '<input type="file" name="photo" id="photo" style="display:none;" />' +
            '<label for="photo" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
            '<label for="photo" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
            '<span class="fileupload-exists btn btn-sm btn-danger" data-dismiss="fileupload" onclick="removePicture(this)">移除</span>' +
            '</div>' +
            '</div>' +
            '<br><br><br>' +
            '</form>' +
            '<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
            '<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
            '<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
            '</div>'
        });
        $(".fileupload").fileupload();
        $("input[name='id']").val(editingData['id']);
        setData($("#admin-editer"), editingData);
    });

});

function showOperator(type, isdeleted, id){
    $.ajax({
        url:"advertise/show.do",
        data:{"type":type,"isdeleted":isdeleted, "id": id},
        success:function(res){
            layer.msg(res.msg);
            update($("#dataTable"));
        }
    });
}

function removePicture() {
    $(".fileupload").fileupload("clear");
}